<script setup lang="ts">
import { getCategoryTopAPI } from '@/services/category'
import { onLoad } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'

// 获取屏幕边界到安全区距离
const { safeAreaInsets } = uni.getSystemInfoSync()

// 搜索的值
const seek = ref('')
// 搜索栏是否显示
const isShow = ref(false)
// 搜索输入
const input = () => {
  isShow.value = true
}
// 搜索框获取焦点时
const focus = () => {
  isShow.value = true
}
// 搜索框失去焦点时
const blur = () => {
  isShow.value = false
}

// 声明搜索的商品
interface Good {
  id: number
  name: string
}

// 搜索栏所有商品列表
const searchList = ref<Good[]>([])
const getCategoryTopData = async () => {
  const res = await getCategoryTopAPI()
  // 查询时，过滤出模糊匹配的数据
  const filtered = res.result.filter((v) => v.children)

  // 将数据对象里嵌套的所有商品扁平化处理并返回一个新的数组
  searchList.value = filtered.flatMap((category) =>
    category.children.flatMap((subCategory) =>
      subCategory.goods.map((good) => ({ id: Number(good.id), name: good.name })),
    ),
  )
}

// 过滤出符合条件的商品
const filterList = computed(() => {
  var arr: any[] = []
  // 展示所有搜索商品(商品过多，没必要先展示所有的)
  // searchList.value.forEach((item) => arr.push(item))
  // 展示过滤后的商品
  if (seek.value) {
    arr = searchList.value.filter((item) => item.name.includes(seek.value))
  }
  return arr
})

// 加载列表
onLoad(() => {
  getCategoryTopData()
})

// 查询
const search = (id: number) => {
  isShow.value = false
  uni.navigateTo({
    url: `/pages/goods/goods?id=${id}`,
  })
}
</script>

<template>
  <view class="navbar" :style="{ paddingTop: safeAreaInsets?.top + 'px' }">
    <!-- logo文字 -->
    <view class="logo">
      <image class="logo-image" src="@/static/images/logo.png"></image>
      <text class="logo-text">新鲜 · 亲民 · 快捷</text>
    </view>
    <!-- 搜索条 -->
    <!-- <view class="search">
      <el-input class="icon-search">搜索商品</el-input>
      <text class="icon-scan"></text>
    </view> -->

    <!-- 搜索栏 -->
    <view class="search">
      <uni-search-bar
        v-model="seek"
        @input="input"
        @focus="focus"
        @blur="blur"
        placeholder="搜索商品"
        bgColor="#80ddcc"
        class="easyInput"
        :radius="8"
      >
        <template v-slot:searchIcon>
          <uni-icons color="#ffffff" size="18" type="search" />
        </template>
      </uni-search-bar>
      <!-- 搜索栏提示框 -->
      <view
        class="InputList"
        v-show="isShow && filterList.length"
        :style="{ top: (safeAreaInsets?.top || 0) + 84 + 'px' }"
      >
        <navigator
          v-for="(item, index) in filterList"
          :key="index"
          class="listSon"
          :url="`/pages/goods/goods?id=${item.id}`"
        >
          {{ item.name }}
        </navigator>
      </view>
    </view>
  </view>
</template>

<style lang="scss">
/* 自定义导航条 */
.navbar {
  background-image: url(@/static/images/navigator_bg.png);
  background-size: cover;
  position: relative;
  display: flex;
  flex-direction: column;
  padding-top: 20px;
  .logo {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;
    padding-top: 20rpx;
    .logo-image {
      width: 166rpx;
      height: 39rpx;
    }
    .logo-text {
      flex: 1;
      line-height: 28rpx;
      color: #fff;
      margin: 2rpx 0 0 20rpx;
      padding-left: 20rpx;
      border-left: 1rpx solid #fff;
      font-size: 26rpx;
    }
  }
  .search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 10rpx;
    /* 搜索框 */
    .easyInput {
      width: 100%;
    }
    /* 搜索列表 */
    .InputList {
      width: 93%;
      height: 480rpx;
      min-height: 200rpx;
      position: absolute;
      left: 31rpx;
      top: 70rpx;
      overflow: hidden;
      overflow-y: scroll;
      border-radius: 10rpx;
      color: black;
      background-color: #ffffff;
      z-index: 1;
    }
    /* 单个搜索数据 */
    .listSon {
      height: 60rpx;
      line-height: 60rpx;
      font-size: 32rpx;
      text-indent: 2em;
      white-space: nowrap; /* 禁止文本换行 */
      overflow: hidden; /* 隐藏超出部分 */
      text-overflow: ellipsis; /* 显示省略号 */
      font-size: 30rpx;
    }
  }
  .icon-search {
    &::before {
      margin-right: 10rpx;
    }
  }
  .icon-scan {
    font-size: 30rpx;
    padding: 15rpx;
  }
}
</style>
